﻿using System.Windows;
using System.Windows.Controls;

namespace 行情显示.Common
{
    public class DataGrid
    {
        public static bool GetShwoRowIndex(DependencyObject obj)
        {
            return (bool)obj.GetValue(ShowRowIndexProperty);
        }

        public static void SetShowRowIndex(DependencyObject obj, bool value)
        {
            obj.SetValue(ShowRowIndexProperty, value);
        }

        public static readonly DependencyProperty ShowRowIndexProperty
            = DependencyProperty.RegisterAttached("ShowRowIndex"
                                                , typeof(bool)
                                                , typeof(DataGrid)
                                                , new UIPropertyMetadata(false, ShowRowIndexPropertyChanged));

        private static void ShowRowIndexPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var dataGrid = d as System.Windows.Controls.DataGrid;
            if (dataGrid == null || !(e.NewValue is bool bl)) return;
            if (bl) dataGrid.LoadingRow += DataGrid_LoadingRow;
            else dataGrid.LoadingRow -= DataGrid_LoadingRow;
        }

        private static void DataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            e.Row.Header = e.Row.GetIndex() + 1;
        }
    }
}
